// JavaScript Document
(function () {
	var category_url = "/categories.do";
	if (context_path) {
		category_url = context_path + category_url;
	}
	var category = window.category = {updateList:function (list, level, values, key) {
		list.html("");
		var ul = $("<ul></ul>").appendTo(list);
		$.each(values, function () {
			var name = this["name"];
			var more = this["more"];
			var key = this["key"];
			$(ul).append($("<li></li>").addClass("item").attr("key", key).attr("more", more).html(name));
		});
		category.initList(list, level, key);
	}, load:function (level, params, key) {
		var list = category.list(level);
		$.ajax({url:category_url, data:(params), type:"GET", dataType:"json", scriptCharset:"utf-8", success:function (msg) {
			var map = msg;
			category.updateList(list, level, map, key);
		}});
		if (level > 1) {
		}
	}, container:function () {
		return $("#cascadeList > ol:first");
	}, showContainer:function () {
		return $("#showContainer");
	}, list:function (level) {
		var ol = category.container();
		var list = ol.find("li[level=" + level + "]");
		if (!list.exists()) {
			list = $("<li level=\"" + level + "\"></li>").addClass("level");
			list.appendTo(ol);
		}
		return list;
	}, initList:function (list, level, key) {
		$("#submit_selected").attr("disabled", "disabled");
		list.find("ul > li").click(function () {
			var li = $(this);
			level = parseInt(level);
			category.onselect(li);
			category.updateSelectView(li);
			if (li.attr("more") == "true") {
				category.load(level + 1, "id=" + li.attr("key"));
				$("#cascadeList > ol > li:gt(" + (level) + ")").remove();
				$("#submit_selected").attr("disabled", "disabled");
			} else {
				$("#cascadeList > ol > li:gt(" + (level - 1) + ")").remove();
				$("#category_id").val(li.attr("key"));
				$("#categoryId").val(li.attr("key")); //diff id
				$("#submit_selected").attr("disabled", "");
			}
		});
		list.find("ul >li[more=true]").addClass("more");
	}, select:function (list, key) {
		if (!key) {
			return;
		}
		var li = list.find("ul > li[key=" + key + "]");
		category.onselect(li);
	}, onselect:function (li) {
		li.parent().find("li").removeClass("selected").addClass("other");
		li.parent().find("li[more=true]").addClass("more");
		li.removeClass("other").removeClass("more").addClass("selected");
		if (li.attr("more") == "true") {
			li.addClass("hasmore");
		}
	}, updateSelectView:function (li) {
		li = $(li);
		var level = category.getLevelByLi(li);
		var showContainer = category.showContainer();
		var preSpan;
		if (level > 1) {
			preSpan = showContainer.find("span[level=" + (level - 1) + "]");
		}
		var targetSpan = showContainer.find("span[level=" + level + "]");
		if (!targetSpan.exists()) {
			targetSpan = $("<span></span>");
			targetSpan.attr("level", level);
			if (level > 1) {
				targetSpan.insertAfter(preSpan);
			} else {
				targetSpan.appendTo(showContainer);
			}
		}
		var text = level > 1 ? " > " + li.text() : li.text();
		targetSpan.text(text);
		targetSpan.show();
		var nextSpans = targetSpan.parent().find("span:gt(" + (level - 1) + ")");
		if (nextSpans.length > 0) {
			for (var i = 0; i < nextSpans.length; i++) {
				$(nextSpans[i]).text("");
			}
		}
	}, getLevelByLi:function (li) {
		var level = $(li).parent().parent().attr("level");
		return parseInt(level);
	}};
})();

